﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Longbow.Cache;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 配置服务注入方法扩展类
    /// </summary>
    public static class CacheManagerServiceCollectionExtensions
    {
        /// <summary>
        /// 增加缓存管理到服务容器中 <see cref="IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
        /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
        public static IServiceCollection AddCacheManager(this IServiceCollection services)
        {
            services.AddHttpContextAccessor();
            services.AddSingleton<IConfigureOptions<CacheOptions>, CacheConfigureOptions<CacheOptions>>();
            services.AddSingleton<IOptionsChangeTokenSource<CacheOptions>, ConfigurationChangeTokenSource<CacheOptions>>();
            services.AddHttpClient(DefaultCache.HttpClientName, client => client.DefaultRequestHeaders.Connection.Add("keep-alive"));
            services.RegisterCacheManager();
            return services;
        }
    }
}
#endif
